package com.example.demo1.demos.web;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sql.rowset.serial.SerialStruct;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/user")
@RestController
public class helloControler {
    @RequestMapping("/hello22")
    public String hello22(){
        return "hello springboot";
    }
    @RequestMapping("/r1")//传递单个参数
    public String r1(String name){
        return "成功接收到参数"+name;
    }
    @RequestMapping("/r3")//传递多个参数
    public String r3(String name,Integer age){
        return "name="+name+",age="+age;
    }
    @RequestMapping("/r4")//传递对象
    public String r4(userInfo user){
        return user.toString();
    }
    @RequestMapping("/r5")//参数进行重命名，设置参数为非必传
    public String r5(@RequestParam(value="name",required = false)String username,Integer age){
     return "username=" +username+",age="+age;
    }
    @RequestMapping("/r6")//传递数据
    public String r6(String[] arr){
        return Arrays.toString(arr);
    }
    @RequestMapping("/r7")//传递集合
    public String r7(@RequestParam("list") List<String> list){//如果需要转成集合，必须使用注解绑定
        // （这个注解要求必传参，不想传的话参考r5
        // ）
        ///默认情况下请求中参数名相同的多个值是封装到数组
        return list.toString()+"size="+list.size();
    }
    @RequestMapping("/r8")//传递json数据
    public String r8(@RequestBody userInfo user){
        return user.toString();
    }
    @RequestMapping("/r9/{articleId}")//传递URL中的参数
    public String r9(@PathVariable Integer articleId){
        return "articleId:"+articleId;
    }
    @RequestMapping("/r10/{articleId}/{name}")//传递URL中的参数
    public String r10(@PathVariable Integer articleId,@PathVariable String name){
        return "articleId:"+articleId+",name:"+name;
    }
    @RequestMapping("/r11")//上传文件
    public String r11(@RequestPart MultipartFile file) throws IOException {
        String filename=file.getOriginalFilename();
        file.transferTo(new File("D:/"+filename));
        return "文件名称+"+file.getOriginalFilename();
    }

}